package com.cn.xact.ty_treasured_book.util.xmlparser;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.cn.xact.ty_treasured_book.config.TYConfigBase;

import android.util.Log;

public class ExtXMLParse extends DefaultHandler{

     /**
      * This class is used to override <DefaultHandler>
      * to parse XML streams or files with SAX method.
      */
     public final static boolean DEBUG = TYConfigBase.XML_DEBUG;
     private final ExtXMLItem myXMLItem = new ExtXMLItem();

     public final static String TAG = "TY_ExtXMLParse";

     public String currentAttr = new String();
     public String currentData = new String();

     public static int itemCnt = 0;
     public static int listCnt = 0;
     public int tmpCnt = 1;
     public final int VALID_PART = 3;

     // Public method to gain the data vector
     public ExtXMLItem getParsedData() {
         return this.myXMLItem;
     }

     @Override
     public void startDocument() throws SAXException {
         // Nothing to do
     }

     @Override
     public void endDocument() throws SAXException {
          // Nothing to do
     }

     /**
      * Some brief SAX procedure
      * startDocument-->startElement-->characters-->endElement-->endDocument
      * NOTE : startElement-->characters-->endElement is just a loop.
      */
     @Override
     public void startElement(String namespaceURI, String localName,
               String qName, Attributes atts) throws SAXException {
          currentAttr = localName;
          if (localName.equals("Request"))
          {
               /**
                * There are 2 request tag types:
                * <Request> and <Request name = ""/>
                */
            if (0 != atts.getLength())
            {
              this.myXMLItem.setRequestName(atts.getValue("name"));
              this.myXMLItem.setRequestValue("REQUEST");
            }
          }
          else if (localName.equals("parameter"))
          {
            // Extract an Attribute
            String attrName = atts.getValue("name");
            this.myXMLItem.setRequestName(attrName);
            String attrValue = atts.getValue("value");
            this.myXMLItem.setRequestValue(attrValue);
           }
          else if (localName.equals("Response"))
          {
            // Nothing to do
          }
          else if (localName.equals("list"))
          {

            String attrName = atts.getValue("name");
            if (null != attrName)
            {
                this.myXMLItem.setRequestName("list_name");
                this.myXMLItem.setRequestValue(attrName);
            }

            String attrSize = atts.getValue("size");
            if (null != attrName)
            {
                this.myXMLItem.setRequestName("list_size");
                this.myXMLItem.setRequestValue(attrSize);
            }
          }
          else if (localName.equals("item"))
          {
            String attrType = atts.getValue("type");
            if (null != attrType)
            {
                if (this.myXMLItem.ifNameExist("item_type"))
                {
                    this.myXMLItem.setRequestName("item_type" + itemCnt);
                }
                else
                {
                    this.myXMLItem.setRequestName("item_type");
                }
                this.myXMLItem.setRequestValue(attrType);
            }

            String attrName = atts.getValue("name");
            String attrValue = atts.getValue("value");

            if ((attrName != null)
               &&(attrName.equals("comment") || attrName.equals("nick") /*|| attrName.equals("time")*/))
            {
                if (this.myXMLItem.ifNameExist(attrName))
                {
                    currentAttr = attrName + itemCnt;
                }
                else
                {
                    currentAttr = attrName;
                }
            }

            if ((null != attrName) && (null != attrValue))
            {
                if (this.myXMLItem.ifNameExist(attrName))
                {
                    this.myXMLItem.setRequestName(attrName + itemCnt);
                }
                else
                {
                    this.myXMLItem.setRequestName(attrName);
                }
                this.myXMLItem.setRequestValue(attrValue);

                if (DEBUG)
                    Log.v(TAG, "Get " + attrName + itemCnt + " = " + attrValue);
            }

          }
          else if (localName.equals("p"))
          {
            String attrName = atts.getValue("name");
            String attrValue = atts.getValue("value");

            if ((attrName != null)
                &&((attrName.equals("name"))
                    || attrName.equals("date")
                    || attrName.equals("msg")
                    || attrName.equals("credit")
                    || attrName.equals("icon_name")
                    || attrName.equals("detail")))
            {
                if (this.myXMLItem.ifNameExist(attrName))
                {
                    currentAttr = attrName + itemCnt;
                }
                else
                {
                    currentAttr = attrName;
                }
            }

            if ((null != attrName)&&(null != attrValue))
            {
                if (this.myXMLItem.ifNameExist(attrName))
                {
                    this.myXMLItem.setRequestName(attrName + itemCnt);
                }
                else
                {
                    this.myXMLItem.setRequestName(attrName);
                }
                this.myXMLItem.setRequestValue(attrValue);
            }
          }
     }

     @Override
     public void endElement(String namespaceURI, String localName, String qName)
               throws SAXException {
         if (localName.equals("list"))
         {
             itemCnt = 0;
         }
         else if (localName.equals("item"))
         {
             listCnt++;
             itemCnt++;
             if (DEBUG) Log.v(TAG, "itemCnt = " + itemCnt + " now!");
             this.myXMLItem.setItem();
         }
         else if (localName.equals("p"))
         {
             if (currentAttr.startsWith("intro"))
             {
                 if (DEBUG) Log.v(TAG, currentAttr + " ends here");
                 this.myXMLItem.setRequestName(currentAttr);
                 this.myXMLItem.setRequestValue(currentData);
                 currentData = new String();
             }
         }
     }

     /**
      * FIXME : Actually speaking, all the string values in attribute should
      *         be set here into class <ExtXMLItem>, however, for convenience
      *         to the reader, all the setting procedures are in <startElement>
      */
     @Override
     public void characters(char ch[], int start, int length) {

         String tmpData = new String(ch, start, length).trim();
         if ((currentAttr.startsWith("name")
                 //|| currentAttr.startsWith("intro")
                 || currentAttr.startsWith("comment")
                 || currentAttr.startsWith("nick")
                 /*|| currentAttr.startsWith("time")*/)
               &&!(this.myXMLItem.ifNameExist(currentAttr)))
         {
             if (VALID_PART == tmpCnt)
             {
                 this.myXMLItem.setRequestName(currentAttr);
                 this.myXMLItem.setRequestValue(tmpData);
             }
             else
             {
                 tmpCnt++;
             }
         }
         else if (this.myXMLItem.ifNameExist(currentAttr))
         {
             tmpCnt = 1;
         }

         if (currentAttr.startsWith("intro"))
         {
             currentData = currentData + tmpData;
         }
     }
    }